﻿using System;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using Microsoft.SharePoint.Taxonomy;

namespace MS.InformationManagement.Documents.ObjectDef.ItemReceivers.DocumentEventReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class DocumentEventReceiver : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being deleted.
       /// </summary>
       public override void ItemDeleting(SPItemEventProperties properties)
       {
           base.ItemDeleting(properties);
           try
           {
               SPListItem element = properties.ListItem;
               switch (element.ContentType.Name)
               {
                   case GlobalConstants.CTTN_ARCHIVE:
                       TaxonomyFieldValue category = element[CustomSiteColumns.FIELD_DOCUMENT_CATEGORY] as TaxonomyFieldValue;
                       if(!category.Label.Equals(GlobalConstants.METADATA_TERM_CATEGORY_RESPONDED)){
                           properties.Cancel = true;
                           properties.ErrorMessage = "El documento que esta intentando eliminar tiene un visto bueno aun no resuelto. No puede eliminar el documento.";
                       }
                       break;
                   case GlobalConstants.CTTN_INFORMATION:
                       break;
                   default:
                       if (element[CustomSiteColumns.FIELD_CURRENT_ACTION] != null)
                       {
                           if (element[CustomSiteColumns.FIELD_CURRENT_ACTION].ToString() != "MoveTo" && (element[CustomSiteColumns.FIELD_CURRENT_ACTION].ToString() != "Delete"))
                           {
                               properties.Cancel = true;
                               properties.ErrorMessage = "Usted no posee los privilegios necesarios para eliminar el documento";
                           }
                       }
                       else
                       {
                           properties.Cancel = true;
                           properties.ErrorMessage = "Usted no posee los privilegios necesarios para eliminar el documento";
                       }
                       break;
               }
               //if ((properties.ListItem[SPBuiltInFieldId.ContentType].ToString() != GlobalConstants.CTTN_INFORMATION))
               //{

               //    if (properties.ListItem[CustomSiteColumns.FIELD_CURRENT_ACTION] != null)
               //    {
               //        if (properties.ListItem[CustomSiteColumns.FIELD_CURRENT_ACTION].ToString() != "MoveTo" && (properties.ListItem[CustomSiteColumns.FIELD_CURRENT_ACTION].ToString() != "Delete"))
               //        {
               //            properties.Cancel = true;
               //            properties.ErrorMessage = "Usted no posee los privilegios necesarios para eliminar el documento";
               //        }
               //    }
               //    else
               //    {
               //        properties.Cancel = true;
               //        properties.ErrorMessage = "Usted no posee los privilegios necesarios para eliminar el documento";
               //    }
               //}
           }
           catch (Exception ex)
           {
               //TODO: manejar excepción
               throw;
           }
           finally { this.EventFiringEnabled = true; properties.ListItem[CustomSiteColumns.FIELD_CURRENT_ACTION] = null; }
       }


    }
}
